package com.galengao.springboot.excel.controller;

import com.alibaba.excel.EasyExcel;
import com.galengao.springboot.excel.enrity.UserDto;
import com.galengao.springboot.excel.event.ExcelListener;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.*;

/**
 * excel 导入导出
 *
 * @author GalenGao
 * @version Id: ExcelController.java, v 0.1 2022/6/5 16:37 GalenGao Exp $$
 */
@RestController
public class ExcelController {


    public static void main(String[] args) {
        String fileName="C:\\Users\\galen_gao\\Desktop\\demo.xlsx";
        LocalDateTime dateTime1 = LocalDateTime.now().plusYears(19).plusDays(23).plusHours(2);
        LocalDateTime dateTime2 = LocalDateTime.now().plusYears(33).plusDays(12).plusHours(8);
        LocalDateTime dateTime3 = LocalDateTime.now().plusYears(55).plusDays(6).plusHours(88);
        LocalDateTime dateTime4 = LocalDateTime.now().plusYears(12).plusDays(3).plusHours(66);

        List<UserDto> userDtos = Arrays.asList(new UserDto("张三", 23, transDateTimeToDate(dateTime1),0,new BigDecimal("78.5"),"优秀"), new UserDto("李四", 24, transDateTimeToDate(dateTime2),1,new BigDecimal("86.34"),"良好"),
                new UserDto("张三", 23, transDateTimeToDate(dateTime3),0,new BigDecimal("77.421"),"及格"), new UserDto("张三", 23, transDateTimeToDate(dateTime4),1,new BigDecimal("88.56"),"极差"));
       // 忽略指定列
        EasyExcel.write(fileName, UserDto.class).excludeColumnFieldNames(Collections.singletonList("remark")).sheet("用户信息表").doWrite(userDtos);

        // 再次去读数据
        EasyExcel.read(fileName,UserDto.class,new ExcelListener()).sheet().doRead();
    }


    public static Date transDateTimeToDate(LocalDateTime dateTime) {
        Instant instant = dateTime.atZone(ZoneId.systemDefault()).toInstant();
        return Date.from(instant);
    }
}
